package cn.iocoder.yudao.module.book.controller.admin.audio;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.audio.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.audio.AudioDO;
import cn.iocoder.yudao.module.book.service.audio.AudioService;

@Tag(name = "管理后台 - 音频")
@RestController
@RequestMapping("/book/audio")
@Validated
public class AudioController {

    @Resource
    private AudioService audioService;

    @PostMapping("/create")
    @Operation(summary = "创建音频")
    @PreAuthorize("@ss.hasPermission('book:audio:create')")
    public CommonResult<Integer> createAudio(@Valid @RequestBody AudioSaveReqVO createReqVO) {
        return success(audioService.createAudio(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新音频")
    @PreAuthorize("@ss.hasPermission('book:audio:update')")
    public CommonResult<Boolean> updateAudio(@Valid @RequestBody AudioSaveReqVO updateReqVO) {
        audioService.updateAudio(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除音频")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:audio:delete')")
    public CommonResult<Boolean> deleteAudio(@RequestParam("id") Integer id) {
        audioService.deleteAudio(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得音频")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:audio:query')")
    public CommonResult<AudioRespVO> getAudio(@RequestParam("id") Integer id) {
        AudioDO audio = audioService.getAudio(id);
        return success(BeanUtils.toBean(audio, AudioRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得音频分页")
    @PreAuthorize("@ss.hasPermission('book:audio:query')")
    public CommonResult<PageResult<AudioRespVO>> getAudioPage(@Valid AudioPageReqVO pageReqVO) {
        PageResult<AudioDO> pageResult = audioService.getAudioPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AudioRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出音频 Excel")
    @PreAuthorize("@ss.hasPermission('book:audio:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAudioExcel(@Valid AudioPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AudioDO> list = audioService.getAudioPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "音频.xls", "数据", AudioRespVO.class,
                        BeanUtils.toBean(list, AudioRespVO.class));
    }

}